<?php
/**
 * 描述：船配-企业列表。
 * @final
 */
final class Exec_Ancillary_Account_List extends Exec
{
    public function Get()
    {
        #绑定数据-经营模式
        View::Assign('Ancillary_SupplyList_Model',DAO_BusinessModel::GetData());
        
        #获取参数
        $type=Functions::UrlArgs('type');

        #定义标量        
        $params=array();
        $contentTitle=NULL;

        if($type){
            $params['type']=$type;
        }
        else{
            $params['type']='latest';
        }

        #执行动作
        switch($type)
        {
            case 'powerful':
                $data=$this->Get_Powerful();
                $contentTitle='强企推荐';
            break;

            case 'class':
                $classID=Functions::UrlArgs('id');
                if(!is_numeric($classID))
                {
                    echo 'error';
                    return;
                }
                $data=$this->Get_Class($classID);
                $params['id']=$classID;
                $contentTitle=DAO_Category::GetNameByID($classID);
                $this->Sub_Class($classID,'class');
            break;

            case 'subclass':
                $subclassID=Functions::UrlArgs('id');
                if(!is_numeric($subclassID))
                {
                    echo 'error';
                    return;
                }
                $data=$this->Get_Subclass($subclassID);
                $params['id']=$subclassID;
                $contentTitle=DAO_Category::GetNameByID($subclassID);
                $this->Sub_Class($subclassID,'subclass');
            break;

            case 'search':
                $contentTitle='搜索企业信息';
                $province=trim(@$_GET['province']);
                $city=trim(@$_GET['city']);
                $model=trim(@$_GET['model']);
                $name=trim(@$_GET['name']);

                $sqlParams=array();
                $sql='select * from account where 1=1 ';
                {
                    if(is_numeric($province)&&(int)$province!=-1)
                    {
                        $sql=$sql.'and Province=? ';
                        $sqlParams[]=$province;
                        $params['province']=$province;
                    }

                    if(is_numeric($city)&&(int)$city!=-1)
                    {
                        $sql=$sql.'and City=? ';
                        $sqlParams[]=$city;
                        $params['city']=$city;
                    }

                    if($model!=''&&$model!=NULL)
                    {
                        $sql=$sql.'and Model=? ';
                        $sqlParams[]=$model;
                        $params['model']=$model;
                    }

                    if(!empty($name))
                    {
                        $sql=$sql.'and CompanyName like ? ';
                        $sqlParams[]='%'.$name.'%';
                        $params['name']=urlencode($name);
                    }
                }

                //过滤条件
                $sql=$sql.'and ( ';
                $sql=$sql.'not exists(select * from account_locked where account.Username=account_locked.Account) ';
                $sql=$sql.'and not exists(select * from account_delete where account.Username=account_delete.Account) ';
                $sql=$sql.'and exists(select * from account_email_verification where account.Username=account_email_verification.Account) ';
                $sql=$sql.') ';
                $sql=$sql.'order by ID desc limit ?,20';
                //echo $sql;
                $data=DB::PGetData($sql,$sqlParams);
            break;

            case 'latest':
                $contentTitle='最新企业信息';                
                $data=$this->Get_Latest();
            break;
        }

        #数据处理
        foreach($data['Record'] as &$row)
        {
            //处理Logo图片路径
            $row['Logo']=Config::PATH_RELATIVE_ATTACHMENT_TEMP.'/'.$row['Logo'];
            //产品数量
            $row['SupplyCount']=0;
            $tmp=DB::PGetRows('select count(*) as SupplyCount from supply where Account=?',array($row['Username']));
            if($tmp){
                $row['SupplyCount']=$tmp[0]['SupplyCount'];
            }
            //产品数量
            $row['Licence']=NULL;
            $tmp=DB::PGetRows('select * from licence where Account=?',array($row['Username']));
            if($tmp){
                $row['Licence']=$tmp;
            }
            //是否VIP
            $tmp=DB::PGetRows('select * from account_vip where Account=?',array(
                $row['Username']
            ));
            if($tmp){
                $row['IsVip']='TRUE';
            }
            else{
                $row['IsVip']='FALSE';
            }
            //商业模式
            $row['Model']=DAO_BusinessModel::GetName($row['Model']);
        }

        #渲染输出
        View::Assign('ContentTitle',$contentTitle);
        View::Assign('Accounts',$data['Record']);
        View::Assign('Params',$params);
        View::Assign('PageMenu',PagerFactory::Create('Common')->Fetch($data,array
        (
            'URL'=>'exec.php?action=ancillary_account_list',
            'IsDetail'=>TRUE,
            'Parameters'=>$params,
            'Rewriter'=>($type=='search') ? NULL:new PagerRewrite('/enterprise/','type,id','html')
        )));
        View::Display('ancillary/account/list.tpl');
    }

    private function Get_Latest()
    {
        $sql='select * from account ';
        $sql=$sql.'where not exists(select * from account_locked where account.Username=account_locked.Account) ';                      //不锁定
        $sql=$sql.'and not exists(select * from account_delete where account.Username=account_delete.Account) ';                        //没删除
        $sql=$sql.'and exists(select * from account_email_verification where account.Username=account_email_verification.Account) ';    //己通过邮件验证
        $sql=$sql.'order by Datetime desc limit ?,20';
        $data=DB::GetData($sql);
        return $data;
    }

    /**
     * 描述：按主营行业大类获取数据。
     * @access private
     */
    private function Get_Class($classID)
    {
        $sql='select * from account ';
        $sql=$sql.'where not exists(select * from account_locked where account.Username=account_locked.Account) ';                      //不锁定
        $sql=$sql.'and not exists(select * from account_delete where account.Username=account_delete.Account) ';                        //没删除
        $sql=$sql.'and exists(select * from account_email_verification where account.Username=account_email_verification.Account) ';    //己通过邮件验证
        $sql=$sql."and ( concat(',',Trade_Class,',') regexp concat(',(',replace(".$classID.",',','|'),'),') )";
        $sql=$sql.'order by Datetime desc limit ?,20';
        $data=DB::GetData($sql);
        return $data;
    }

    /**
     * 描述：按小类获取数据。
     * @access private
     */
    private function Get_Subclass($subclassID)
    {
        $sql='select * from account ';
        $sql=$sql.'where not exists(select * from account_locked where account.Username=account_locked.Account) ';                      //不锁定
        $sql=$sql.'and not exists(select * from account_delete where account.Username=account_delete.Account) ';                        //没删除
        $sql=$sql.'and exists(select * from account_email_verification where account.Username=account_email_verification.Account) ';    //己通过邮件验证
        $sql=$sql."and ( concat(',',Trade_Subclass,',') regexp concat(',(',replace(".$subclassID.",',','|'),'),') )";
        $sql=$sql.'order by Datetime desc limit ?,20';
        $data=DB::GetData($sql);
        return $data;
    }

    /**
     * 描述：强企推荐。
     * @access private
     */
    private function Get_Powerful()
    {
        $sql='select * from account ';
        $sql=$sql.'where not exists(select * from account_locked where account.Username=account_locked.Account) ';                      //不锁定
        $sql=$sql.'and not exists(select * from account_delete where account.Username=account_delete.Account) ';                        //没删除
        $sql=$sql.'and exists(select * from account_email_verification where account.Username=account_email_verification.Account) ';    //己通过邮件验证
        $sql=$sql.'and exists(select * from account_powerful where account.Username=account_powerful.Account) ';                        //己通过强企推荐
        $sql=$sql.'order by Datetime desc limit ?,20';
        $data=DB::GetData($sql);
        return $data;
    }

    /**
     * 描述：子过程-绑定类别数据。
     * @access private
     */
    private function Sub_Class($classID,$type)
    {
        #定义标量
        $fieldName=($type=='class') ? 'Trade_Class':'Trade_Subclass';

        #SQL语句
        $sql='select * from trade where Identifying_Parent='.$classID.' order by ID asc';
        if($type=='subclass')
        {
            $tmp=DB::GetRowByID('trade','Identifying:'.$classID);
            if($tmp){
                $sql='select * from trade where Identifying_Parent='.$tmp['Identifying_Parent'].' order by ID asc';
            }
        }
        #获取数据
        $data=DB::GetRows($sql);
        if($data)
        {
           
            foreach($data as &$row)
            {
                $sql='select count(*) as AccountCount from account ';
                $sql=$sql.'where not exists(select * from account_locked where account.Username=account_locked.Account) ';                      //不锁定
                $sql=$sql.'and not exists(select * from account_delete where account.Username=account_delete.Account) ';                        //没删除
                $sql=$sql.'and exists(select * from account_email_verification where account.Username=account_email_verification.Account) ';    //己通过邮件验证
                $sql=$sql."and ( concat(',',".$fieldName.",',') regexp concat(',(',replace(".$row['Identifying'].",',','|'),'),') )";
                $sql=$sql.'order by Datetime desc';
                $row['AccountCount']=0;                
                $tmp=DB::GetRows($sql);
                if($tmp){
                    $row['AccountCount']=$tmp[0]['AccountCount'];
                }
            }
        }

        #绑定模板
        View::Assign('Class',$data);
    }
}
?>